﻿@model Matrix.MaKaYunShang.Model.SysConfig
@{
    ViewBag.Title = "系统设置";
    ViewBag.PathToBtn = "/SysConfig/Detail";
    ViewBag.TextBtn = "刷新";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                系统设置
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Save", "SysConfig", FormMethod.Post, new { id = "frmCreate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-lg-12">
                            <fieldset>
                                <div class="form-group col-md-6">
                                    <label>是否发送店员红包</label>
                                    <br />
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendRedEnvelopeToDelivery, "true") 是
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendRedEnvelopeToDelivery, "false") 否
                                    </label>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店员红包每日总金额</label>
                                    @Html.TextBoxFor(model => model.DayMaxRedEnvolpoe, new { @class = "form-control", placeholder = "店员红包每日总金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店员红包最小值</label>
                                    @Html.TextBoxFor(model => model.MinRedEnvolope, new { @class = "form-control", placeholder = "店员红包最小值" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店员红包最大值</label>
                                    @Html.TextBoxFor(model => model.MaxRedEnvolope, new { @class = "form-control", placeholder = "店员红包最大值" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否支持在线支付下单立减</label>
                                    <br />
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsPayDiscount, "true") 是
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsPayDiscount, "false") 否
                                    </label>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否支持用户第一次关注发送优惠券</label>
                                    <br />
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendCouponOnFirstSubscribe, "true") 是
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendCouponOnFirstSubscribe, "false") 否
                                    </label>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>在线支付每日立减总金额</label>
                                    @Html.TextBoxFor(model => model.DayMaxPayDiscount, new { @class = "form-control", placeholder = "在线支付每日立减总金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>立减订单金额最低门槛</label>
                                    @Html.TextBoxFor(model => model.MinPayValueOfOrder, new { @class = "form-control", placeholder = "立减订单金额最低门槛" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否发送分享红包</label>
                                    <br />
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendShareRedEnvolope, "true") 是
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsSendShareRedEnvolope, "false") 否
                                    </label>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>每日分享红包总金额</label>
                                    @Html.TextBoxFor(model => model.DayMaxCountShareRedEnvolope, new { @class = "form-control", placeholder = "每日分享红包总金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>分享红包最低金额</label>
                                    @Html.TextBoxFor(model => model.MinRedEnvolopeOfShare, new { @class = "form-control", placeholder = "分享红包最低金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>分享红包最高金额</label>
                                    @Html.TextBoxFor(model => model.MaxRedEnvolopeOfShare, new { @class = "form-control", placeholder = "分享红包最高金额" })
                                </div>

                                <div class="form-group col-md-6">
                                    <label>最小提现金额</label>
                                    @Html.TextBoxFor(model => model.MinCashWithdraw, new { @class = "form-control", placeholder = "最小提现金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店铺搜索半径（米）</label>
                                    @Html.TextBoxFor(model => model.SearchShopRadius, new { @class = "form-control", placeholder = "店铺搜索半径（米）" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>平台现金发送警报值</label>
                                    @Html.TextBoxFor(model => model.CashSendRedLine, new { @class = "form-control", placeholder = "平台现金发送警报值" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>线上支付订单最长支付时间（单位分）</label>
                                    @Html.TextBoxFor(model => model.TimeToCancelPayOrder, new { @class = "form-control", placeholder = "线上支付订单最长支付时间" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>终结订单时间长度（单位小时）</label>
                                    @Html.TextBoxFor(model => model.TimeToFinishOrder, new { @class = "form-control", placeholder = "终结订单时间长度" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>商户端订单信息提醒间隔时间（单位分）</label>
                                    @Html.TextBoxFor(model => model.OrderMsgMcAlertInterval, new { @class = "form-control", placeholder = "商户端订单信息提醒间隔时间" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>抢单有效时长 （单位分）</label>
                                    @Html.TextBoxFor(model => model.OrderGrapTimeLength, new { @class = "form-control", placeholder = "抢单有效时长" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>每1元可发卡次数</label>
                                    @Html.TextBoxFor(model => model.CardQuantityPerYuan, new { @class = "form-control", placeholder = "每1元可发卡次数" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>平台分润金额</label>
                                    @Html.TextBoxFor(model => model.PlatformShareAmount, new { @class = "form-control", placeholder = "平台分润金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店铺分润金额</label>
                                    @Html.TextBoxFor(model => model.ShopShareAmount, new { @class = "form-control", placeholder = "店铺分润金额" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>固定码扫码间隔时间(分)</label>
                                    @Html.TextBoxFor(model => model.FixedQrCodeScanTime, new { @class = "form-control", placeholder = "固定码扫码间隔时间" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>总部券占比（百分比）</label>
                                    @Html.TextBoxFor(model => model.HeadquartersCardsPercent, new { @class = "form-control", placeholder = "总部券占比（发卡时计算）" })
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否开启商城</label>
                                    <br />
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsOpenMall, "true") 是
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        @Html.RadioButtonFor(model => model.IsOpenMall, "false") 否
                                    </label>
                                </div>
                                <div class="col-md-12">
                                    <button type="button" class="btn btn-primary" id="btnSave">保存设置</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <script type="text/javascript">
        $(function () {
            $("#frmCreate").validate({
                rules: {
                    MinCashWithdraw: { required: true, min: 0, digits: true },
                    SearchShopRadius: { required: true, min: 0, digits: true },
                    CashSendRedLine: { required: true, min: 0 },
                    TimeToCancelPayOrder: { required: true, min: 0.1, number: true },
                    TimeToFinishOrder: { required: true, min: 0.1, number: true },
                    OrderMsgMcAlertInterval: { required: true, min: 0.1, number: true },
                    OrderGrapTimeLength: { required: true, min: 0.1, number: true },
                    CardQuantityPerYuan: { required: true, min: 1, digits: true },
                    FixedQrCodeScanTime: { required: true, min: 0, digits: true },
                    PlatformShareAmount: { required: true, min: 0, number: true },
                    ShopShareAmount: { required: true, min: 0, number: true },
                    HeadquartersCardsPercent: { required: true, min: 0, max: 100, digits: true }
                },
                messages: {
                    MinCashWithdraw: { required: "请填写最小提现金额" },
                    SearchShopRadius: { required: "请填写分享红包最高金额" },
                    CashSendRedLine: { required: "请填写平台现金发送警报值" },
                    TimeToCancelPayOrder: { required: "请填写平台现金发送警报值" },
                    TimeToFinishOrder: { required: "请填写平台现金发送警报值" },
                    OrderMsgMcAlertInterval: { required: "请填写平台现金发送警报值" },
                    OrderGrapTimeLength: { required: "请填写平台现金发送警报值" },
                    CardQuantityPerYuan: { required: "请填写每1元可发卡次数" },
                    FixedQrCodeScanTime: { required: "请填写固定码扫码间隔时间" },
                    PlatformShareAmount: { required: "请填写平台分润金额" },
                    ShopShareAmount: { required: "请填写店铺分润金额" },
                    HeadquartersCardsPercent: { required: "请填写总部券占比" }
                }
            });

            $("#btnSave").click(function () {
                if ($("#frmCreate").valid()) {
                    if ($("input[name='IsSendRedEnvelopeToDelivery']:checked").val() == "true") {
                        if (parseFloat($("#MaxRedEnvolope").val()) < parseFloat($("#MinRedEnvolope").val())) {
                            alertify.error("店员红包最大值不可小于店员红包最小值");
                            $("#MaxRedEnvolope").focus();
                            return false;
                        }
                        if (parseFloat($("#DayMaxRedEnvolpoe").val()) < parseFloat($("#MinRedEnvolope").val())) {
                            alertify.error("店员红包每日总金额不可小于店员红包最小值");
                            $("#DayMaxRedEnvolpoe").focus();
                            return false;
                        }
                    }
                    if ($("input[name='IsPayDiscount']:checked").val() == "true") {
                        if (parseFloat($("#DayMaxPayDiscount").val()) < parseFloat($("#MinPayValueOfOrder").val())) {
                            alertify.error("在线支付每日立减总金额不可小于立减订单金额最低门槛");
                            $("#DayMaxPayDiscount").focus();
                            return false;
                        }
                    }
                    if ($("input[name='IsSendShareRedEnvolope']:checked").val() == "true") {
                        if (parseFloat($("#MaxRedEnvolopeOfShare").val()) < parseFloat($("#MinRedEnvolopeOfShare").val())) {
                            alertify.error("分享红包最高金额不可小于分享红包最低金额");
                            $("#MaxRedEnvolopeOfShare").focus();
                            return false;
                        }
                        if (parseFloat($("#DayMaxCountShareRedEnvolope").val()) < parseFloat($("#MinRedEnvolopeOfShare").val())) {
                            alertify.error("每日分享红包总金额不可小于分享红包最低金额");
                            $("#DayMaxCountShareRedEnvolope").focus();
                            return false;
                        }
                    }
                    $.ajax({
                        type: 'post',
                        url: '@Url.Action("Save")',
                        data: $("#frmCreate").serialize(),
                        success: function (data) {
                            if (data.Status) {
                                alertify.success("保存成功");
                            } else {
                                alertify.error(data.Message);
                            }
                        },
                        error: function () {
                            alertify.error("保存失败，请重试！");
                        }
                    });
                }
            });

            $("input[name='IsSendRedEnvelopeToDelivery']").change(function () {
                isSendRedEnvelopeToDeliveryChange();
            });

            $("input[name='IsPayDiscount']").change(function () {
                isPayDiscountChange();
            });

            $("input[name='IsSendShareRedEnvolope']").change(function () {
                isSendShareRedEnvolopeChange();
            });

            isSendRedEnvelopeToDeliveryChange();
            isPayDiscountChange();
            isSendShareRedEnvolopeChange();
        });

        function isSendRedEnvelopeToDeliveryChange() {
            if ($("input[name='IsSendRedEnvelopeToDelivery']:checked").val() == "true") {
                $("#DayMaxRedEnvolpoe").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写店员红包每日总金额"
                    }
                });
                $("#MinRedEnvolope").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写店员红包最小值"
                    }
                });
                $("#MaxRedEnvolope").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写店员红包最大值"
                    }
                });
            } else {
                $("#DayMaxRedEnvolpoe").rules("remove");
                $("#MinRedEnvolope").rules("remove");
                $("#MaxRedEnvolope").rules("remove");
            }
        }

        function isPayDiscountChange() {
            if ($("input[name='IsPayDiscount']:checked").val() == "true") {
                $("#DayMaxPayDiscount").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写每日最大立减总金额"
                    }
                });
                $("#MinPayValueOfOrder").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写立减订单金额最低门槛"
                    }
                });
            } else {
                $("#DayMaxPayDiscount").rules("remove");
                $("#MinPayValueOfOrder").rules("remove");
            }
        }

        function isSendShareRedEnvolopeChange() {
            if ($("input[name='IsSendShareRedEnvolope']:checked").val() == "true") {
                $("#DayMaxCountShareRedEnvolope").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写每日分享红包最大金额"
                    }
                });
                $("#MinRedEnvolopeOfShare").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写分享红包最低金额"
                    }
                });
                $("#MaxRedEnvolopeOfShare").rules("add", {
                    required: true,
                    min: 0,
                    messages: {
                        required: "请填写分享红包最高金额"
                    }
                });
            } else {
                $("#DayMaxCountShareRedEnvolope").rules("remove");
                $("#MinRedEnvolopeOfShare").rules("remove");
                $("#MaxRedEnvolopeOfShare").rules("remove");
            }
        }
    </script>
}